//
//  TBRBusinessTableView.m
//  TBRentClient
//
//  Created by tonbright on 2018/4/26.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRBusinessRentView.h"

#import "TBRBusinessRentGetMessageCell.h"
#import "TBRCouponMessageCell.h"

#import "TBRHomeModel.h"
#import <YYModel.h>
#import "BRPickerView.h"

#import "TBRChosseCarMessageC.h"
#import "TBRRentPersonMessageC.h"
#import "TBROnCarPlaceC.h"
#import "AppSetting.h"

@interface TBRBusinessRentView()<UITableViewDelegate,UITableViewDataSource>
@end

@implementation TBRBusinessRentView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorColor = [UIColor lightGrayColor];
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.rowHeight = UITableViewAutomaticDimension;
        self.estimatedRowHeight = 44;
        [self registerNib:[UINib nibWithNibName:@"TBRBusinessRentGetMessageCell" bundle:nil] forCellReuseIdentifier:@"TBRBusinessRentGetMessageCellId"];
        [self registerNib:[UINib nibWithNibName:@"TBRCouponMessageCell" bundle:nil] forCellReuseIdentifier:@"TBRCouponMessageCellId"];
        NSArray *tmpArray = @[@{@"messageId":@"time",@"imageName":@"",@"titleName":@"请选择上车时间"},
                           @{@"messageId":@"onPlace",@"imageName":@"",@"titleName":@"您在哪上车"},
                           @{@"messageId":@"offPlace",@"imageName":@"",@"titleName":@"您在哪下车"},
                           @{@"messageId":@"carMessage",@"imageName":@"",@"titleName":@"请选择您的乘车"},
                           @{@"messageId":@"personMessage",@"imageName":@"",@"titleName":@"请选择乘车人信息"},
                           ];
        self.dataArray = [NSArray yy_modelArrayWithClass:[TBRRentGetMessageModel class] json:tmpArray];
    }
    return self;
}



#pragma mark - 实现数据源方法
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? self.dataArray.count : 1;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        TBRCouponMessageCell *cellCouponMessage = [tableView dequeueReusableCellWithIdentifier:@"TBRCouponMessageCellId" forIndexPath:indexPath];
        [cellCouponMessage.remarkText addMaxTextLengthWithMaxLength:50 andEvent:^(BRPlaceholderTextView *text) {
        }];
        [cellCouponMessage.remarkText addTextViewEndEvent:^(BRPlaceholderTextView *text) {
            
        }];
        cellCouponMessage.remarkText.placeholder = @"选填,可告知您的其他需求(最多50字)";
        return cellCouponMessage;
    }
    TBRBusinessRentGetMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TBRBusinessRentGetMessageCellId" forIndexPath:indexPath];
    //设置数据
    TBRRentGetMessageModel *model = self.dataArray[indexPath.row];
    //返回
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        return;
    }
    //设置数据
    TBRRentGetMessageModel *model = self.dataArray[indexPath.row];
    //返回
    TBRBusinessRentGetMessageCell * cell = [self cellForRowAtIndexPath:indexPath];
    //使用id防止后续出现上下调换顺序的情况-------------
    __weak TBRBusinessRentGetMessageCell * weakCell = cell;
    if ([model.messageId isEqualToString:@"time"]) {
        NSDate *minDate = [NSDate date];
        NSDate *maxDate = [NSDate setYear:2300 month:3 day:3];
        [BRDatePickerView showDatePickerWithTitle:@"出生日期" dateType:BRDatePickerModeYMDHM defaultSelValue:[NSDate currentDateStringWithFormat:@"yyyy-MM-dd HH:mm"] minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
            //给模型赋值-----
            
            //给cell赋值
            weakCell.lbe_title.text = selectValue;
            model.detailName = selectValue;
            [AppSetting shareAppSetting].allMessageModel.onCarTime = selectValue;
        } cancelBlock:^{
            NSLog(@"点击了背景或取消按钮");
        }];
    }else if ([model.messageId isEqualToString:@"onPlace"] || [model.messageId isEqualToString:@"offPlace"]) {
        //跳转到一个新控制器--------
        TBROnCarPlaceC *onCarPlaceC = [[TBROnCarPlaceC alloc] init];
        onCarPlaceC.onCarPlaceBlock = ^(TBROnCarPlaceModel *carPlaceModel) {
            weakCell.lbe_title.text = carPlaceModel.titleStr;
            model.detailName = carPlaceModel.titleStr;
            if ([model.messageId isEqualToString:@"onPlace"]) {
                [AppSetting shareAppSetting].allMessageModel.onPlace = carPlaceModel.titleStr;
            }else {
                [AppSetting shareAppSetting].allMessageModel.offPlace = carPlaceModel.titleStr;
            }
        };
        if ([model.messageId isEqualToString:@"onPlace"]) {
            onCarPlaceC.navigationItem.title = @"请选择上车地点";
        }else {
            onCarPlaceC.navigationItem.title = @"请选择下车地点";
        }
        [[HelpObject getCurrentNav] pushViewController:onCarPlaceC animated:YES];
        //新控制器的block返回的时候取出值------
        
    }else if ([model.messageId isEqualToString:@"carMessage"]) {
        TBRChosseCarMessageC *chosseCarMessageC = [[TBRChosseCarMessageC alloc] init];
        [[HelpObject getCurrentNav] pushViewController:chosseCarMessageC animated:YES];
        
        chosseCarMessageC.CarMessageBlock = ^(NSInteger index) {
            //选中了车辆额======
            NSLog(@"选中了车辆额");
        };
        
    }else if ([model.messageId isEqualToString:@"personMessage"]) {
        TBRRentPersonMessageC *rentPersonMC = [[TBRRentPersonMessageC alloc] init];
        [[HelpObject getCurrentNav] pushViewController:rentPersonMC animated:YES];
        
        //确认按钮点击时候吧值传递回来
        
    }
}

@end
